//
//  YHWelcomeViewController.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/23.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SDWebImage
class YHWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

    }
    
    override func loadView() {
        view = backView
    }
    private func setupUI() {
        
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        iconView.sd_setImageWithURL(YHUserAccountViewModel.sharedAccountViewModel.iconURL)
        
        iconView.cornerRadius = 45
        
        nameLabel.alpha = 0
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation() {
        
        let offset = -screenHight + 160
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: {
            
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            
            self.view.layoutIfNeeded()
            }) { (_) in
                
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
//                         print("ok")
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                })
               
        }
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named:"ad_background"))
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    
    private lazy var nameLabel : UILabel = UILabel(title: (YHUserAccountViewModel.sharedAccountViewModel.userAccount?.name)!, font: 18, textColor: UIColor.darkGrayColor())


}
